import os,sys
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
from conan.tools.system import package_manager
import platform

manager_mapping = {"Apt": ["Linux", "ubuntu", "debian", "raspbian", "linuxmint", 'astra', 'elbrus', 'altlinux', 'pop'],
                    "Apk": ["alpine"],
                    "Yum": ["pidora", "scientific", "xenserver", "amazon", "oracle", "amzn",
                            "almalinux", "rocky"],
                    "Dnf": ["fedora", "rhel", "centos", "mageia", "nobara"],
                    "Brew": ["Darwin"],
                    "PacMan": ["arch", "manjaro", "msys2", "endeavouros"],
                    "Chocolatey": ["Windows"],
                    "Zypper": ["opensuse", "sles"],
                    "Pkg": ["freebsd"],
                    "Pkgutil": ["Solaris"]}

class helloRecipe(ConanFile):
    name = "RimeInstall"
    version = "0.1"
    package_type = "build-scripts"

    # 个人信息
    license = "MIT"
    author = "<OSmile> <whghcyx@outlook.com>"
    url = "https://whg555.github.io"
    description = "readme description"
    topics = ("<Put some tag here>", "<here>", "<and here>")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False], "fPIC": [True, False]}
    default_options = {"shared": False, "fPIC": True}

    # Sources are located in the same place as this recipe, copy them to the recipe
    exports_sources = "CMakeLists.txt", "src/*", "include/*"

    def config_options(self):
        '''
        针对系统，对配置命令做一些修改
        <1>conan install .
        <1>conan create .
        <1>conan build .
        <test><1>conan create .
        '''
        print("\033[43;34mOSmile-->", "config_options", "\033[0m")

    def configure(self):
        '''
        <2>conan install .
        <2>conan create .
        <2>conan build .
        <test><2>conan create .
        '''
        print("\033[43;34mOSmile-->", "configure", "\033[0m")
        self.conf.define("tools.system.package_manager:mode", "install")
        self.conf.define("tools.system.package_manager:sudo", True)


    def layout(self):
        '''
        <3>conan install .
        <3>conan create .
        <3>conan build .
        <test><3>conan create .
        '''
        print("\033[43;34mOSmile-->", "layout", "\033[0m")

    def system_requirements(self):
        '''
        安装需要的软件
        <4>conan install .
        <4>conan create .
        <4>conan build .
        <test><4>conan create .
        '''
        print("\033[43;34mOSmile-->", "system_requirements", "\033[0m")
        os_name = platform.system()
        if os_name in ["Linux", "FreeBSD"]:
            import distro
            os_name = distro.id() or os_name
            os_version = distro.version(best=True).split(".")[0]
            print("OS", os_name)
            print("Version", os_version)
            # 获取编译工具
            for tool, distros in manager_mapping.items():
                if os_name in distros:
                    break
            softs = ["ibus-rime", "librime-data-luna-pinyin"]
            print("安装软件包-->", softs)
            softtool = getattr(package_manager, tool)(self)
            softtool.install(softs, update=False, check=True)
        elif os_name == "Windows" and self._conanfile.settings.get_safe("os.subsystem") == "msys2":
            os_name = "msys2"
        print(os_name)
    
    def generate(self):
        '''
        配置已经安装的软件
        <5>conan install .
        <5>conan create .
        <5>conan build .
        '''
        print("\033[43;34mOSmile-->", "generate", "\033[0m")
        # rime输入法
        self.run("git clone https://github.com/KyleBing/rime-wubi86-jidian.git", cwd=os.environ["HOME"])
        self.run("git checkout 3e7483f19eadbc87bafceb36beb01c8643d1f4c2", cwd=os.path.join(os.environ["HOME"], "rime-wubi86-jidian"))
        self.run("mkdir -p .config/ibus/rime", cwd=os.environ["HOME"])
        self.run("mv -f rime-wubi86-jidian/* .config/ibus/rime/", cwd=os.environ["HOME"])
        print("重启系统后配置输入法、、、")
        print("设置->区域与语言->加号->汉语->汉语(Rime)->添加")

    def build(self):
        '''
        <6>conan create .
        <6>conan build .
        '''
        print("\033[43;34mOSmile-->", "build", "\033[0m")

    def package(self):
        '''
        <7>conan create .
        '''
        print("\033[43;34mOSmile-->", "package", "\033[0m")

    def package_info(self):
        '''
        <8>conan create .
        <test><5>conan create .
        '''
        print("\033[43;34mOSmile-->", "package_info", "\033[0m")

